// pages/myinfo/myinfo.js
const app = getApp();
import common from "../../assets/common.js"
import api from "../../api/Api.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myinfo: {
      imageSrc: ""
    },
    survey: {
      notdone: "0",
      done: "0"
    },
    myserverinfo: {},
    phoneValidate: {
      show: false
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    _this.login();
    app.wxGetUserInfo(() => {
      _this.setData({
        'myinfo.imageSrc': app.globalData.userInfo.avatarUrl
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    if (app.globalData.userVo && app.globalData.userVo.id) {
      _this.getSurveyCount();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 手机验证OK回调事件
   */
  handlePhoneValidateOk(e) {
    let _this = this;
    let userVo = app.globalData.userVo ? app.globalData.userVo : {};
    userVo.phone = e.detail ? e.detail.phone : "";
    userVo.validateCode = e.detail ? e.detail.validateCode : "";
    common.serverRegister(userVo).then(res => {
      console.log("serverRegister success");
      console.log(res);
      _this.setData({
        myserverinfo: res,
        'phoneValidate.show': false
      });
      _this.getSurveyCount();
    }).catch(() => {});
  },
  /**
   * 手机验证Cancel回调事件
   */
  handlePhoneValidateCancel(e) {
    let _this = this;
    _this.setData({
      'phoneValidate.show': false
    });
  },
  /**
   * 登录注册点击事件
   */
  handleLogin() {
    let _this = this;
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          _this.setData({
            'phoneValidate.show': true
          });
        }
      }
    });
  },
  handleGetUserInfo({detail}) {
    let _that = this;
    app.globalData.userInfo = detail.userInfo;
    _that.setData({
      'myinfo.imageSrc': app.globalData.userInfo.avatarUrl,
      'phoneValidate.show': true
    });
  },
  /**
   * 登录注册
   */
  login() {
    let _this = this;
    common.serverLogin().then(res => {
      console.log("serverLogin success");
      console.log(res);
      if (!res) {
        return;
      }
      _this.setData({ myserverinfo: res});
      _this.getSurveyCount();
      // wx.redirectTo({
      //   url: '../questionnaire/questionnaire?usersurveyid=8aa7a8fd6f0c3828016f0c3e98950012&surveyname=测试1'
      // })
    }).catch(e => {
      console.warn(e);
    });
  },

  /**
   * 获取已完成未完成调查统计
   */
  getSurveyCount() {
    let _this = this;
    let params = {
      userId: app.globalData.userVo ? app.globalData.userVo.id : ""
    }
    api.survey.getSurveyCount(params).then(res => {
      console.log("getSurveyCount success");
      console.log(res);
      res.forEach(v => {
        if (v.isDone) {
          _this.data.survey.done = v.sum ? v.sum : 0;
        } else {
          _this.data.survey.notdone = v.sum ? v.sum : 0;
        }
      });
      _this.setData({
        survey: _this.data.survey
      });
    }).catch(e => {
      console.warn(e);
    })
  }
})